home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / packages / tar-mode.el < prev    next >
Encoding:
Text File  |  1995-04-26  |  53.4 KB  |  1,403 lines

  1. ;;; tar-mode.el --- simple editing of tar files from GNU emacs
  2. ;; Keywords: unix
  3.  
  4. ;;; File:        tar-mode.el
  5. ;;; Description:    simple editing of tar files from GNU emacs
  6. ;;; Author:        Jamie Zawinski <jwz@lucid.com>
  7. ;;; Created:        4 Apr 1990
  8. ;;; Version:        1.31, 15 Dec 93
  9.  
  10. ;;; Copyright (C) 1990-1993 Free Software Foundation, Inc.
  11. ;;;
  12. ;; This file is part of XEmacs.
  13.  
  14. ;; XEmacs is free software; you can redistribute it and/or modify it
  15. ;; under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation; either version 2, or (at your option)
  17. ;; any later version.
  18.  
  19. ;; XEmacs is distributed in the hope that it will be useful, but
  20. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  21. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  22. ;; General Public License for more details.
  23.  
  24. ;; You should have received a copy of the GNU General Public License
  25. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  26. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  27.  
  28. ;;; This package attempts to make dealing with Unix 'tar' archives easier.
  29. ;;; When this code is loaded, visiting a file whose name ends in '.tar' will
  30. ;;; cause the contents of that archive file to be displayed in a Dired-like
  31. ;;; listing.  It is then possible to use the customary Dired keybindings to
  32. ;;; extract sub-files from that archive, either by reading them into their own
  33. ;;; editor buffers, or by copying them directly to arbitrary files on disk.
  34. ;;; It is also possible to delete sub-files from within the tar file and write
  35. ;;; the modified archive back to disk, or to edit sub-files within the archive
  36. ;;; and re-insert the modified files into the archive.  See the documentation
  37. ;;; string of tar-mode for more info.
  38.  
  39. ;;; To autoload, add this to your .emacs file:
  40. ;;;
  41. ;;;  (setq auto-mode-alist (cons '("\\.tar$" . tar-mode) auto-mode-alist))
  42. ;;;  (autoload 'tar-mode "tar-mode")
  43. ;;;
  44. ;;; But beware: for certain tar files - those whose very first file has 
  45. ;;; a -*- property line - autoloading won't work.  See the function 
  46. ;;; "tar-normal-mode" to understand why.
  47.  
  48. ;;; This code now understands the extra fields that GNU tar adds to tar files.
  49.  
  50. ;;; This interacts correctly with "uncompress.el" in the Emacs library,
  51. ;;; and with sufficiently recent versions of "crypt.el" by Kyle Jones.
  52.  
  53. ;;;    ***************   TO DO   *************** 
  54. ;;;
  55. ;;; o  There should be a command to extract the whole current buffer into files
  56. ;;;    on disk (right now you have to do the subfiles one at a time.)
  57. ;;;
  58. ;;; o  chmod should understand "a+x,og-w".
  59. ;;;
  60. ;;; o  It's not possible to add a NEW file to a tar archive; not that 
  61. ;;;    important, but still...
  62. ;;;
  63. ;;; o  The code is less efficient that it could be - in a lot of places, I
  64. ;;;    pull a 512-character string out of the buffer and parse it, when I could
  65. ;;;    be parsing it in place, not garbaging a string.  Should redo that.
  66. ;;;
  67. ;;; o  I'd like a command that searches for a string/regexp in every subfile
  68. ;;;    of an archive, where <esc> would leave you in a subfile-edit buffer.
  69. ;;;    (Like M-s in VM and M-r in the Zmacs mail reader.)
  70. ;;;
  71. ;;; o  Sometimes (but not always) reverting the tar-file buffer does not 
  72. ;;;    re-grind the listing, and you are staring at the binary tar data.
  73. ;;;    Typing 'g' again immediately after that will always revert and re-grind
  74. ;;;    it, though.  I have no idea why this happens.
  75. ;;;
  76. ;;; o  If you edit a subfile, and selective-display gets set to t, then when
  77. ;;;    we save the subfile, we should map ^M -> ^J.
  78. ;;;
  79. ;;; o  Block files, sparse files, continuation files, and the various header
  80. ;;;    types aren't editable.  Actually I don't know that they work at all.
  81. ;;;    If you know that they work, or know that they don't, please let me know.
  82. ;;;
  83. ;;; o  Tar files inside of tar files don't work.
  84. ;;;
  85. ;;; o  When using crypt-mode, you can't save a compressed or encrypted subfile
  86. ;;;    of a tar file back into the tar file: it is saved uncompressed.
  87.  
  88. (defvar tar-anal-blocksize 20
  89.   "*The blocksize of tar files written by Emacs, or nil, meaning don't care.
  90. The blocksize of a tar file is not really the size of the blocks; rather, it is
  91. the number of blocks written with one system call.  When tarring to a tape, 
  92. this is the size of the *tape* blocks, but when writing to a file, it doesn't
  93. matter much.  The only noticeable difference is that if a tar file does not
  94. have a blocksize of 20, the tar program will issue a warning; all this really
  95. controls is how many null padding bytes go on the end of the tar file.")
  96.  
  97. (defvar tar-update-datestamp (or (fboundp 'current-time)
  98.                  (fboundp 'current-time-seconds))
  99.   "*Whether tar-mode should play fast and loose with sub-file datestamps;
  100. if this is true, then editing and saving a tar file entry back into its
  101. tar file will update its datestamp.  If false, the datestamp is unchanged.
  102. You may or may not want this - it is good in that you can tell when a file
  103. in a tar archive has been changed, but it is bad for the same reason that
  104. editing a file in the tar archive at all is bad - the changed version of 
  105. the file never exists on disk.
  106.  
  107. This does not work in Emacs 18, because there's no way to get the current 
  108. time as an integer - if this var is true, then editing a file sets its date
  109. to midnight, Jan 1 1970 GMT, which happens to be what 0 encodes.")
  110.  
  111.  
  112. ;;; First, duplicate some Common Lisp functions; I used to just (require 'cl)
  113. ;;; but "cl.el" was messing some people up (also it's really big).
  114.  
  115. (defmacro tar-setf (form val)
  116.   "A mind-numbingly simple implementation of setf."
  117.   (let ((mform (macroexpand form (and (boundp 'byte-compile-macro-environment)
  118.                       byte-compile-macro-environment))))
  119.     (cond ((symbolp mform) (list 'setq mform val))
  120.       ((not (consp mform)) (error "can't setf %s" form))
  121.       ((eq (car mform) 'aref)
  122.        (list 'aset (nth 1 mform) (nth 2 mform) val))
  123.       ((eq (car mform) 'car)
  124.        (list 'setcar (nth 1 mform) val))
  125.       ((eq (car mform) 'cdr)
  126.        (list 'setcdr (nth 1 mform) val))
  127.       (t (error "don't know how to setf %s" form)))))
  128.  
  129. (defmacro tar-dolist (control &rest body)
  130.   "syntax: (dolist (var-name list-expr &optional return-value) &body body)"
  131.   (let ((var (car control))
  132.     (init (car (cdr control)))
  133.     (val (car (cdr (cdr control)))))
  134.     (list 'let (list (list '_dolist_iterator_ init))
  135.       (list 'while '_dolist_iterator_
  136.         (cons 'let
  137.           (cons (list (list var '(car _dolist_iterator_)))
  138.             (append body
  139.                 (list (list 'setq '_dolist_iterator_
  140.                     (list 'cdr '_dolist_iterator_)))))))
  141.       val)))
  142.  
  143. (defmacro tar-dotimes (control &rest body)
  144.   "syntax: (dotimes (var-name count-expr &optional return-value) &body body)"
  145.   (let ((var (car control))
  146.     (n (car (cdr control)))
  147.     (val (car (cdr (cdr control)))))
  148.     (list 'let (list (list '_dotimes_end_ n)
  149.              (list var 0))
  150.       (cons 'while
  151.         (cons (list '< var '_dotimes_end_)
  152.               (append body
  153.                   (list (list 'setq var (list '1+ var))))))
  154.       val)))
  155.  
  156.  
  157. ;;; down to business.
  158.  
  159. (defmacro make-tar-header (name mode uid git size date ck lt ln
  160.                magic uname gname devmaj devmin)
  161.   (list 'vector name mode uid git size date ck lt ln
  162.     magic uname gname devmaj devmin))
  163.  
  164. (defmacro tar-header-name (x) (list 'aref x 0))
  165. (defmacro tar-header-mode (x) (list 'aref x 1))
  166. (defmacro tar-header-uid  (x) (list 'aref x 2))
  167. (defmacro tar-header-gid  (x) (list 'aref x 3))
  168. (defmacro tar-header-size (x) (list 'aref x 4))
  169. (defmacro tar-header-date (x) (list 'aref x 5))
  170. (defmacro tar-header-checksum  (x) (list 'aref x 6))
  171. (defmacro tar-header-link-type (x) (list 'aref x 7))
  172. (defmacro tar-header-link-name (x) (list 'aref x 8))
  173. (defmacro tar-header-magic (x) (list 'aref x 9))
  174. (defmacro tar-header-uname (x) (list 'aref x 10))
  175. (defmacro tar-header-gname (x) (list 'aref x 11))
  176. (defmacro tar-header-dmaj (x) (list 'aref x 12))
  177. (defmacro tar-header-dmin (x) (list 'aref x 13))
  178.  
  179. (defmacro make-tar-desc (data-start tokens)
  180.   (list 'cons data-start tokens))
  181.  
  182. (defmacro tar-desc-data-start (x) (list 'car x))
  183. (defmacro tar-desc-tokens     (x) (list 'cdr x))
  184.  
  185. (defconst tar-name-offset 0)
  186. (defconst tar-mode-offset (+ tar-name-offset 100))
  187. (defconst tar-uid-offset  (+ tar-mode-offset 8))
  188. (defconst tar-gid-offset  (+ tar-uid-offset 8))
  189. (defconst tar-size-offset (+ tar-gid-offset 8))
  190. (defconst tar-time-offset (+ tar-size-offset 12))
  191. (defconst tar-chk-offset  (+ tar-time-offset 12))
  192. (defconst tar-linkp-offset (+ tar-chk-offset 8))
  193. (defconst tar-link-offset (+ tar-linkp-offset 1))
  194. ;;; GNU-tar specific slots.
  195. (defconst tar-magic-offset (+ tar-link-offset 100))
  196. (defconst tar-uname-offset (+ tar-magic-offset 8))
  197. (defconst tar-gname-offset (+ tar-uname-offset 32))
  198. (defconst tar-dmaj-offset (+ tar-gname-offset 32))
  199. (defconst tar-dmin-offset (+ tar-dmaj-offset 8))
  200. (defconst tar-end-offset (+ tar-dmin-offset 8))
  201.  
  202. (defun tokenize-tar-header-block (string)
  203.   "Returns a 'tar-header' structure (a list of name, mode, uid, gid, size, 
  204. write-date, checksum, link-type, and link-name)."
  205.   (cond ((< (length string) 512) nil)
  206.     (;(some 'plusp string)         ; <-- oops, massive cycle hog!
  207.      (or (not (= 0 (aref string 0))) ; This will do.
  208.          (not (= 0 (aref string 101))))
  209.      (let* ((name-end (1- tar-mode-offset))
  210.         (link-end (1- tar-magic-offset))
  211.         (uname-end (1- tar-gname-offset))
  212.         (gname-end (1- tar-dmaj-offset))
  213.         (link-p (aref string tar-linkp-offset))
  214.         (magic-str (substring string tar-magic-offset (1- tar-uname-offset)))
  215.         (uname-valid-p (or (string= "ustar  " magic-str) (string= "GNUtar " magic-str)))
  216.         name
  217.         (nulsexp   "[^\000]*\000"))
  218.        (and (string-match nulsexp string tar-name-offset) (setq name-end (min name-end (1- (match-end 0)))))
  219.        (and (string-match nulsexp string tar-link-offset) (setq link-end (min link-end (1- (match-end 0)))))
  220.        (and (string-match nulsexp string tar-uname-offset) (setq uname-end (min uname-end (1- (match-end 0)))))
  221.        (and (string-match nulsexp string tar-gname-offset) (setq gname-end (min gname-end (1- (match-end 0)))))
  222.        (setq name (substring string tar-name-offset name-end)
  223.          link-p (if (or (= link-p 0) (= link-p ?0))
  224.                 nil
  225.               (- link-p ?0)))
  226.        (if (and (null link-p) (string-match "/$" name)) (setq link-p 5)) ; directory
  227.        (make-tar-header
  228.          name
  229.          (tar-parse-octal-integer string tar-mode-offset (1- tar-uid-offset))
  230.          (tar-parse-octal-integer string tar-uid-offset (1- tar-gid-offset))
  231.          (tar-parse-octal-integer string tar-gid-offset (1- tar-size-offset))
  232.          (tar-parse-octal-integer string tar-size-offset (1- tar-time-offset))
  233.          (tar-parse-octal-integer-32 string tar-time-offset (1- tar-chk-offset))
  234.          (tar-parse-octal-integer string tar-chk-offset (1- tar-linkp-offset))
  235.          link-p
  236.          (substring string tar-link-offset link-end)
  237.          uname-valid-p
  238.          (and uname-valid-p (substring string tar-uname-offset uname-end))
  239.          (and uname-valid-p (substring string tar-gname-offset gname-end))
  240.          (tar-parse-octal-integer string tar-dmaj-offset (1- tar-dmin-offset))
  241.          (tar-parse-octal-integer string tar-dmin-offset (1- tar-end-offset))
  242.          )))
  243.     (t 'empty-tar-block)))
  244.  
  245.  
  246. (defun tar-parse-octal-integer (string &optional start end)
  247.   "deletes all your files, and then reboots."
  248.   (if (null start) (setq start 0))
  249.   (if (null end) (setq end (length string)))
  250.   (if (= (aref string start) 0)
  251.       0
  252.     (let ((n 0))
  253.       (while (< start end)
  254.     (setq n (if (< (aref string start) ?0) n
  255.           (+ (* n 8) (- (aref string start) 48)))
  256.           start (1+ start)))
  257.       n)))
  258.  
  259. (defun tar-parse-octal-integer-32 (string &optional start end)
  260.   ;; like tar-parse-octal-integer, but returns a cons of two 16-bit numbers,
  261.   ;; since elisp can't handle integers of that magnitude.
  262.   (or start (setq start 0))
  263.   (or end (setq end (length string)))
  264.   (let ((top (tar-parse-octal-integer string start (- end 6)))
  265.     (bot (tar-parse-octal-integer string (- end 6) end)))
  266.     (setq top (logior (ash top 2) (ash bot -16)))
  267.     (setq bot (logand bot 65535))
  268.     (cons top bot)))
  269.  
  270. (defun tar-parse-octal-integer-safe (string)
  271.   (let ((L (length string)))
  272.     (if (= L 0) (error "empty string"))
  273.     (tar-dotimes (i L)
  274.        (if (or (< (aref string i) ?0)
  275.            (> (aref string i) ?7))
  276.        (error "'%c' is not an octal digit."))))
  277.   (tar-parse-octal-integer string))
  278.  
  279.  
  280. (defun checksum-tar-header-block (string)
  281.   "Computes and returns a tar-acceptable checksum for this block."
  282.   (let* ((chk-field-start tar-chk-offset)
  283.      (chk-field-end (+ chk-field-start 8))
  284.      (sum 0)
  285.      (i 0))
  286.     ;; Add up all of the characters except the ones in the checksum field.
  287.     ;; Add that field as if it were filled with spaces.
  288.     (while (< i chk-field-start)
  289.       (setq sum (+ sum (aref string i))
  290.         i (1+ i)))
  291.     (setq i chk-field-end)
  292.     (while (< i 512)
  293.       (setq sum (+ sum (aref string i))
  294.         i (1+ i)))
  295.     (+ sum (* 32 8))))
  296.  
  297. (defun check-tar-header-block-checksum (hblock desired-checksum file-name)
  298.   "Beep and print a warning if the checksum doesn't match."
  299.   (if (not (= desired-checksum (checksum-tar-header-block hblock)))
  300.       (progn (beep) (message "Invalid checksum for file %s!" file-name))))
  301.  
  302. (defun recompute-tar-header-block-checksum (hblock)
  303.   "Modifies the given string to have a valid checksum field."
  304.   (let* ((chk (checksum-tar-header-block hblock))
  305.      (chk-string (format "%6o" chk))
  306.      (l (length chk-string)))
  307.     (aset hblock 154 0)
  308.     (aset hblock 155 32)
  309.     (tar-dotimes (i l) (aset hblock (- 153 i) (aref chk-string (- l i 1)))))
  310.   hblock)
  311.  
  312.  
  313. (defun tar-grind-file-mode (mode string start)
  314.   "Write a \"-rw--r--r-\" representing MODE into STRING beginning at START."
  315.   (aset string start       (if (zerop (logand 256 mode)) ?- ?r))
  316.   (aset string (+ start 1) (if (zerop (logand 128 mode)) ?- ?w))
  317.   (aset string (+ start 2) (if (zerop (logand  64 mode)) ?- ?x)) 
  318.   (aset string (+ start 3) (if (zerop (logand  32 mode)) ?- ?r))
  319.   (aset string (+ start 4) (if (zerop (logand  16 mode)) ?- ?w))
  320.   (aset string (+ start 5) (if (zerop (logand   8 mode)) ?- ?x))
  321.   (aset string (+ start 6) (if (zerop (logand   4 mode)) ?- ?r))
  322.   (aset string (+ start 7) (if (zerop (logand   2 mode)) ?- ?w))
  323.   (aset string (+ start 8) (if (zerop (logand   1 mode)) ?- ?x))
  324.   (if (zerop (logand 1024 mode)) nil (aset string (+ start 2) ?s))
  325.   (if (zerop (logand 2048 mode)) nil (aset string (+ start 5) ?s))
  326.   string)
  327.  
  328.  
  329. (defconst tar-can-print-dates (or (fboundp 'current-time)
  330.                   (fboundp 'current-time-seconds))
  331.   "true if this emacs has been built with time-printing support")
  332.  
  333. (defun summarize-tar-header-block (tar-hblock &optional mod-p)
  334.   "Returns a line similar to the output of 'tar -vtf'."
  335.   (let ((name (tar-header-name tar-hblock))
  336.     (mode (tar-header-mode tar-hblock))
  337.     (uid (tar-header-uid tar-hblock))
  338.     (gid (tar-header-gid tar-hblock))
  339.     (uname (tar-header-uname tar-hblock))
  340.     (gname (tar-header-gname tar-hblock))
  341.     (size (tar-header-size tar-hblock))
  342.     (time (tar-header-date tar-hblock))
  343.     (ck (tar-header-checksum tar-hblock))
  344.     (link-p (tar-header-link-type tar-hblock))
  345.     (link-name (tar-header-link-name tar-hblock))
  346.     )
  347.     (let* ((left 11)
  348.        (namew 8)
  349.        (groupw 8)
  350.        (sizew 8)
  351.        (datew (if tar-can-print-dates 15 2))
  352.        (slash (1- (+ left namew)))
  353.        (lastdigit (+ slash groupw sizew))
  354.        (namestart (+ lastdigit datew))
  355.        (string (make-string (+ namestart (length name) (if link-p (+ 5 (length link-name)) 0)) 32))
  356.        (type (tar-header-link-type tar-hblock)))
  357.       (aset string 0 (if mod-p ?* ? ))
  358.       (aset string 1
  359.         (cond ((or (eq type nil) (eq type 0)) ?-)
  360.           ((eq type 1) ?l)  ; link
  361.           ((eq type 2) ?s)  ; symlink
  362.           ((eq type 3) ?c)  ; char special
  363.           ((eq type 4) ?b)  ; block special
  364.           ((eq type 5) ?d)  ; directory
  365.           ((eq type 6) ?p)  ; FIFO/pipe
  366.           ((eq type 20) ?*) ; directory listing
  367.           ((eq type 29) ?M) ; multivolume continuation
  368.           ((eq type 35) ?S) ; sparse
  369.           ((eq type 38) ?V) ; volume header
  370.           ))
  371.       (tar-grind-file-mode mode string 2)
  372.       (setq uid (if (= 0 (length uname)) (int-to-string uid) uname))
  373.       (setq gid (if (= 0 (length gname)) (int-to-string gid) gname))
  374.       (setq size (int-to-string size))
  375.       (tar-dotimes (i (min (1- namew) (length uid))) (aset string (- slash i) (aref uid (- (length uid) i 1))))
  376.       (aset string (1+ slash) ?/)
  377.       (tar-dotimes (i (min (1- groupw) (length gid))) (aset string (+ (+ slash 2) i) (aref gid i)))
  378.       (tar-dotimes (i (min sizew (length size))) (aset string (- lastdigit i) (aref size (- (length size) i 1))))
  379.  
  380.       (if tar-can-print-dates
  381.       (let* ((year (substring (current-time-string) -4))
  382.          ;; in v18, current-time-string doesn't take an argument
  383.          (file (current-time-string time))
  384.          (file-year (substring file -4))
  385.          (str (if (equal year file-year)
  386.               (substring file 4 16)
  387.             (concat (substring file 4 11) " " file-year))))
  388.         (tar-dotimes (i 12) (aset string (- namestart (- 13 i)) (aref str i)))))
  389.  
  390.       (tar-dotimes (i (length name)) (aset string (+ namestart i) (aref name i)))
  391.       (if (or (eq link-p 1) (eq link-p 2))
  392.       (progn
  393.         (tar-dotimes (i 3) (aset string (+ namestart 1 (length name) i) (aref (if (= link-p 1) "==>" "-->") i)))
  394.         (tar-dotimes (i (length link-name)) (aset string (+ namestart 5 (length name) i) (aref link-name i)))))
  395.       string)))
  396.  
  397.  
  398. ;; buffer-local variables in the tar file's buffer:
  399. ;;
  400. (defvar tar-parse-info)        ; the header structures
  401. (defvar tar-header-offset)    ; the end of the "pretty" data
  402.  
  403. (defun tar-summarize-buffer ()
  404.   "Parse the contents of the tar file in the current buffer, and place a
  405. dired-like listing on the front; then narrow to it, so that only that listing
  406. is visible (and the real data of the buffer is hidden)."
  407.   (message "parsing tar file...")
  408.   (let* ((result '())
  409.      (pos 1)
  410.      (bs (max 1 (- (buffer-size) 1024))) ; always 2+ empty blocks at end.
  411.      (bs100 (max 1 (/ bs 100)))
  412.     (tokens nil))
  413.     (while (not (eq tokens 'empty-tar-block))
  414.       (if (> (+ pos 512) (point-max))
  415.       (error "truncated tar file"))
  416.       (let* ((hblock (buffer-substring pos (+ pos 512))))
  417.     (setq tokens (tokenize-tar-header-block hblock))
  418.     (setq pos (+ pos 512))
  419.     (message "parsing tar file...%s%%"
  420.          ;(/ (* pos 100) bs)   ; this gets round-off lossage
  421.          (/ pos bs100)         ; this doesn't
  422.          )
  423.     (if (eq tokens 'empty-tar-block)
  424.         nil
  425.       (if (null tokens) (error "premature EOF parsing tar file."))
  426.       (if (eq (tar-header-link-type tokens) 20)
  427.           ;; Foo.  There's an extra empty block after these.
  428.           (setq pos (+ pos 512)))
  429.       (let ((size (tar-header-size tokens)))
  430.         (if (< size 0)
  431.         (error "%s has size %s - corrupted."
  432.                (tar-header-name tokens) size))
  433.         ;
  434.         ; This is just too slow.  Don't really need it anyway....
  435.         ;(check-tar-header-block-checksum
  436.         ;  hblock (checksum-tar-header-block hblock)
  437.         ;  (tar-header-name tokens))
  438.         
  439.         (setq result (cons (make-tar-desc pos tokens) result))
  440.         
  441.         (if (and (null (tar-header-link-type tokens))
  442.              (> size 0))
  443.         (setq pos
  444.           (+ pos 512 (ash (ash (1- size) -9) 9))        ; this works
  445.           ;(+ pos (+ size (- 512 (rem (1- size) 512)))) ; this doesn't
  446.           ))
  447.         ))))
  448.     (make-local-variable 'tar-parse-info)
  449.     (setq tar-parse-info (nreverse result)))
  450.   (message "parsing tar file...formatting...")
  451.   (save-excursion
  452.     (goto-char (point-min))
  453.     (let ((buffer-read-only nil))
  454.       (tar-dolist (tar-desc tar-parse-info)
  455.     (insert
  456.       (summarize-tar-header-block (tar-desc-tokens tar-desc))
  457.       "\n"))
  458.       (make-local-variable 'tar-header-offset)
  459.       (setq tar-header-offset (point))
  460.       (narrow-to-region 1 tar-header-offset)
  461.       (set-buffer-modified-p nil)))
  462.   (message "parsing tar file...done."))
  463.  
  464.  
  465. (defvar tar-mode-map nil "*Local keymap for tar-mode listings.")
  466.  
  467. (if tar-mode-map
  468.     nil
  469.   (setq tar-mode-map (make-keymap))
  470.   (suppress-keymap tar-mode-map)
  471.   (define-key tar-mode-map " " 'tar-next-line)
  472.   (define-key tar-mode-map "c" 'tar-copy)
  473.   (define-key tar-mode-map "d" 'tar-flag-deleted)
  474.   (define-key tar-mode-map "\^D" 'tar-flag-deleted)
  475.   (define-key tar-mode-map "e" 'tar-extract)
  476.   (define-key tar-mode-map "f" 'tar-extract)
  477.   (define-key tar-mode-map "g" 'revert-buffer)
  478.   (define-key tar-mode-map "h" 'describe-mode)
  479.   (define-key tar-mode-map "n" 'tar-next-line)
  480.   (define-key tar-mode-map "\^N" 'tar-next-line)
  481.   (define-key tar-mode-map "o" 'tar-extract-other-window)
  482.   (define-key tar-mode-map "\^C" 'tar-copy)
  483.   (define-key tar-mode-map "p" 'tar-previous-line)
  484.   (define-key tar-mode-map "\^P" 'tar-previous-line)
  485.   (define-key tar-mode-map "r" 'tar-rename-entry)
  486.   (define-key tar-mode-map "u" 'tar-unflag)
  487.   (define-key tar-mode-map "v" 'tar-view)
  488.   (define-key tar-mode-map "x" 'tar-expunge)
  489.   (define-key tar-mode-map "\177" 'tar-unflag-backwards)
  490.   (define-key tar-mode-map "E" 'tar-extract-other-window)
  491.   (define-key tar-mode-map "M" 'tar-chmod-entry)
  492.   (define-key tar-mode-map "G" 'tar-chgrp-entry)
  493.   (define-key tar-mode-map "O" 'tar-chown-entry)
  494.  
  495.   (cond ((string-match "XEmacs" emacs-version)
  496.      (define-key tar-mode-map 'button2 'tar-track-mouse-and-extract-file)
  497.      (define-key tar-mode-map 'button3 'tar-popup-menu)))
  498.   )
  499.  
  500.  
  501. ;; XEmacs menu mouse/support added by Heiko Muenkel
  502. ;; muenkel@tnt.uni-hannover.de
  503.  
  504. (autoload 'dired-mark-region "dired-xemacs-menu")
  505.  
  506. (defvar tar-menu
  507.   '("Tar Mode Commands"
  508.     ["Copy Subfile to Disk" tar-copy t]
  509.     ["Rename Subfile" tar-rename-entry t]
  510.     "----"
  511.     ["Delete Flaged Subfiles" tar-expunge t]
  512.     ["Flag Subfile for Deletion" tar-flag-deleted t]
  513.     ["Flag Subfiles in Region for Deletion"
  514.      (dired-mark-region '(tar-flag-deleted 1))
  515.      (mark)]
  516.     ["Unflag Subfile" tar-unflag t]
  517.     ["Unflag Subfiles in Region"
  518.      (dired-mark-region '(tar-flag-deleted 1 t))
  519.      (mark)]
  520.     "----"
  521.     ["Change Permissions of Subfile..." tar-chmod-entry t]
  522.     ["Change Group of Subfile..." tar-chgrp-entry t]
  523.     ["Change Owner of Subfile..." tar-chown-entry t]
  524.     "----"
  525.     ["Edit Subfile Other Window" tar-extract-other-window t]
  526.     ["Edit Subfile" tar-extract t]
  527.     ["View Subfile" tar-view t]
  528.     ))
  529.  
  530.  
  531. (defun tar-track-mouse-and-extract-file (event)
  532.   "Visit the tar-file-entry upon which the mouse is clicked."
  533.   (interactive "e")
  534.   (mouse-set-point event)
  535.   (tar-next-line 0)
  536.   (let (buffer)
  537.     (save-excursion
  538.       (tar-extract)
  539.       (setq buffer (current-buffer)))
  540.     (switch-to-buffer buffer)))
  541.  
  542. (defun tar-popup-menu (event)
  543.   "Display the tar-mode menu."
  544.   (interactive "@e")
  545.   (mouse-set-point event)
  546.   (tar-next-line 0)
  547.   (popup-menu tar-menu))
  548.  
  549.  
  550. ;; tar mode is suitable only for specially formatted data.
  551. (put 'tar-mode 'mode-class 'special)
  552. (put 'tar-subfile-mode 'mode-class 'special)
  553.  
  554. ;;;###autoload
  555. (defun tar-mode ()
  556.   "Major mode for viewing a tar file as a dired-like listing of its contents.
  557. You can move around using the usual cursor motion commands. 
  558. Letters no longer insert themselves.
  559. Type 'e' to pull a file out of the tar file and into its own buffer.
  560. Type 'c' to copy an entry from the tar file into another file on disk.
  561.  
  562. If you edit a sub-file of this archive (as with the 'e' command) and 
  563. save it with Control-X Control-S, the contents of that buffer will be 
  564. saved back into the tar-file buffer; in this way you can edit a file 
  565. inside of a tar archive without extracting it and re-archiving it.
  566.  
  567. See also: variables tar-update-datestamp and tar-anal-blocksize.
  568. \\{tar-mode-map}"
  569.   ;; this is not interactive because you shouldn't be turning this
  570.   ;; mode on and off.  You can corrupt things that way.
  571.   (make-local-variable 'tar-header-offset)
  572.   (make-local-variable 'tar-parse-info)
  573.   (make-local-variable 'require-final-newline)
  574.   (setq require-final-newline nil) ; binary data, dude...
  575.   (make-local-variable 'revert-buffer-function)
  576.   (setq revert-buffer-function 'tar-mode-revert)
  577.   (setq major-mode 'tar-mode)
  578.   (setq mode-name "Tar")
  579.   (use-local-map tar-mode-map)
  580.   (auto-save-mode 0)
  581.   (widen)
  582.   (if (and (boundp 'tar-header-offset) tar-header-offset)
  583.       (narrow-to-region 1 tar-header-offset)
  584.       (tar-summarize-buffer))
  585.  
  586.   (cond ((string-match "XEmacs" emacs-version)
  587.      (require 'mode-motion)
  588.      (setq mode-motion-hook 'mode-motion-highlight-line)
  589.      (if (and current-menubar (not (assoc "Tar" current-menubar)))
  590.          (progn
  591.            (set-buffer-menubar (copy-sequence current-menubar))
  592.            (add-menu nil "Tar" (cdr tar-menu))))
  593.      ))
  594.   (run-hooks 'tar-mode-hook)
  595.   )
  596.  
  597. ;; buffer-local variables in subfile mode.
  598. ;;
  599. (defvar tar-subfile-mode nil)        ; whether the minor-mode is on
  600. (defvar superior-tar-buffer)        ; parent buffer
  601. (defvar superior-tar-descriptor)    ; header object of this file
  602. (defvar tar-subfile-buffer-id)        ; pretty name-string
  603. (defvar subfile-orig-mlbid)        ; orig mode-line-buffer-identification
  604.  
  605. (defun tar-subfile-mode (p)
  606.   "Minor mode for editing an element of a tar-file.
  607. This mode redefines ^X^S to save the current buffer back into its 
  608. associated tar-file buffer.  You must save that buffer to actually
  609. save your changes to disk."
  610.   (interactive "P")
  611.   (or (and (boundp 'superior-tar-buffer) superior-tar-buffer)
  612.       (error "This buffer is not an element of a tar file."))
  613.   (or (assq 'tar-subfile-mode minor-mode-alist)
  614.       (setq minor-mode-alist (append minor-mode-alist
  615.                      (list '(tar-subfile-mode " TarFile")))))
  616.   (make-local-variable 'tar-subfile-mode)
  617.   (setq tar-subfile-mode
  618.     (if (null p)
  619.         (not tar-subfile-mode)
  620.         (> (prefix-numeric-value p) 0)))
  621.   (cond (tar-subfile-mode
  622.      ;; copy the local keymap so that we don't accidentally
  623.      ;; alter a keymap like 'lisp-mode-map' which is shared
  624.      ;; by all buffers in that mode.
  625.      (let ((m (current-local-map)))
  626.        (if m (use-local-map (copy-keymap m))))
  627.      (local-set-key "\^X\^S" 'tar-subfile-save-buffer)
  628.      ;; turn off auto-save.
  629.      (auto-save-mode nil)
  630.      (setq buffer-auto-save-file-name nil)
  631.      (run-hooks 'tar-subfile-mode-hook))
  632.     (t
  633.      ;; remove the local binding for C-x C-s.
  634.      (local-unset-key "\^X\^S")
  635.      (if subfile-orig-mlbid
  636.          (set (make-local-variable 'mode-line-buffer-identification)
  637.           subfile-orig-mlbid))
  638.      (setq superior-tar-buffer nil
  639.            superior-tar-descriptor nil
  640.            subfile-orig-mlbid nil)
  641.      ))
  642.   )
  643.  
  644. (defun tar-subfile-after-write-file-hook ()
  645.   ;; if the buffer has a filename, then it is no longer associated with
  646.   ;; the tar file.  Turn off subfile mode.
  647.   (if (and buffer-file-name tar-subfile-mode)
  648.       (tar-subfile-mode -1)))
  649.  
  650. (defun tar-mode-revert (&optional no-autosave no-confirm)
  651.   "Revert this buffer and turn on tar mode again, to re-compute the
  652. directory listing."
  653.   (setq tar-header-offset nil)
  654.   (let ((revert-buffer-function nil))
  655.     (revert-buffer t no-confirm)
  656.     (widen))
  657.   (tar-mode))
  658.  
  659.  
  660. (defun tar-next-line (p)
  661.   (interactive "p")
  662.   (forward-line p)
  663.   (if (eobp) nil (forward-char (if tar-can-print-dates 48 36))))
  664.  
  665. (defun tar-previous-line (p)
  666.   (interactive "p")
  667.   (tar-next-line (- p)))
  668.  
  669. (defun tar-current-descriptor (&optional noerror)
  670.   "Returns the tar-descriptor of the current line, or signals an error."
  671.   ;; I wish lines had plists, like in ZMACS...
  672.   (or (nth (count-lines (point-min)
  673.             (save-excursion (beginning-of-line) (point)))
  674.        tar-parse-info)
  675.       (if noerror
  676.       nil
  677.       (error "This line does not describe a tar-file entry."))))
  678.  
  679.  
  680. (defun tar-extract (&optional other-window-p)
  681.   "*In tar-mode, extract this entry of the tar file into its own buffer."
  682.   (interactive)
  683.   (let* ((view-p (eq other-window-p 'view))
  684.      (descriptor (tar-current-descriptor))
  685.      (tokens (tar-desc-tokens descriptor))
  686.      (name (tar-header-name tokens))
  687.      (size (tar-header-size tokens))
  688.      (link-p (tar-header-link-type tokens))
  689.      (start (+ (tar-desc-data-start descriptor) tar-header-offset -1))
  690.      (end (+ start size)))
  691.     (if link-p
  692.     (error "This is a %s, not a real file."
  693.            (cond ((eq link-p 5) "directory")
  694.              ((eq link-p 20) "tar directory header")
  695.              ((eq link-p 29) "multivolume-continuation")
  696.              ((eq link-p 35) "sparse entry")
  697.              ((eq link-p 38) "volume header")
  698.              (t "link"))))
  699.     (if (zerop size) (error "This is a zero-length file."))
  700.     (let* ((tar-buffer (current-buffer))
  701.        (bufname (file-name-nondirectory name))
  702.        (bufid (concat ;" (" name " in "
  703.                  " (in "
  704.               (file-name-nondirectory (buffer-file-name))
  705.               ")"))
  706.        (read-only-p (or buffer-read-only view-p))
  707.        (buffer nil)
  708.        (buffers (buffer-list))
  709.        (just-created nil))
  710.       ;; find a buffer visiting this subfile from this tar file.
  711.       (while (and buffers (not buffer))
  712.     (set-buffer (car buffers))
  713.     (if (and (null (buffer-file-name (car buffers)))
  714.          (boundp 'superior-tar-descriptor)
  715.          (eq superior-tar-descriptor descriptor))
  716.         (setq buffer (car buffers))
  717.         (setq buffers (cdr buffers))))
  718.       (set-buffer tar-buffer)
  719.       (if buffer
  720.       nil
  721.     (setq buffer (generate-new-buffer bufname))
  722.     (setq just-created t)
  723.     (unwind-protect
  724.         (progn
  725.           (widen)
  726.           (save-excursion
  727.         (set-buffer buffer)
  728.         (insert-buffer-substring tar-buffer start end)
  729.         (goto-char 0)
  730.         (let ((lock-directory nil)) ; disable locking
  731.           (set-visited-file-name name) ; give it a name to decide mode.
  732. ;;          (normal-mode)  ; pick a mode.
  733. ;;          (after-find-file nil nil)  ; pick a mode; works with crypt.el
  734.           ;; Ok, instead of running after-find-file, just invoke the
  735.           ;; find-file-hooks instead.  This does everything we want
  736.           ;; from after-find-file, without losing when visiting .tar
  737.           ;; files via ange-ftp: doesn't probe the ftp site for the
  738.           ;; name of the subfile.
  739.           (normal-mode t)
  740.           (run-hooks 'find-file-hooks)
  741.           (set-visited-file-name nil) ; nuke the name - not meaningful.
  742.           )
  743.         (make-local-variable 'superior-tar-buffer)
  744.         (make-local-variable 'superior-tar-descriptor)
  745.         (make-local-variable 'mode-line-buffer-identification)
  746.         (make-local-variable 'tar-subfile-buffer-id)
  747.         (make-local-variable 'subfile-orig-mlbid)
  748.         (setq superior-tar-buffer tar-buffer)
  749.         (setq superior-tar-descriptor descriptor)
  750.         (setq tar-subfile-buffer-id bufid)
  751.         (setq subfile-orig-mlbid mode-line-buffer-identification)
  752.         (cond ((stringp mode-line-buffer-identification)
  753.                (setq mode-line-buffer-identification
  754.                  (list mode-line-buffer-identification))))
  755.         (let ((ms (car mode-line-buffer-identification))
  756.               n)
  757.           (cond ((and (stringp ms)
  758.                   (string-match "%\\([0-9]+\\)b\\'" ms))
  759.              (setq mode-line-buffer-identification
  760.                    (cons
  761.                 (concat (substring ms 0
  762.                            (1- (match-beginning 1)))
  763.                     (substring ms (1+ (match-end 1))))
  764.                 (cons
  765.                  (list (car (read-from-string
  766.                          (substring ms (match-beginning 1)
  767.                             (match-end 1))))
  768.                        (concat "%b" tar-subfile-buffer-id))
  769.                  (cdr mode-line-buffer-identification)))))
  770.             (t
  771.              (setq mode-line-buffer-identification
  772.                    (list "Emacs: "
  773.                      (list 17
  774.                        (concat "%b"
  775.                            tar-subfile-buffer-id)))))))
  776.         (tar-subfile-mode 1)
  777.         
  778.         (setq buffer-read-only read-only-p)
  779.         (set-buffer-modified-p nil))
  780.           (set-buffer tar-buffer))
  781.       (narrow-to-region 1 tar-header-offset)))
  782.       (if view-p
  783.       (progn
  784.         (view-buffer-other-window buffer)
  785.         (save-excursion
  786.           (set-buffer buffer)
  787.           ;; for view-less.el; view.el can't do this.
  788.           (set (make-local-variable 'view-kill-on-exit) t)))
  789.     (if other-window-p
  790.         (switch-to-buffer-other-window buffer)
  791.       (switch-to-buffer buffer))))))
  792.  
  793.  
  794. (defun tar-extract-other-window ()
  795.   "*In tar-mode, extract this entry of the tar file into its own buffer."
  796.   (interactive)
  797.   (tar-extract t))
  798.  
  799. (defun tar-view ()
  800.   "*In tar-mode, view the tar file entry on this line."
  801.   (interactive)
  802.   (tar-extract 'view))
  803.  
  804.  
  805. (defun tar-read-file-name (&optional prompt)
  806.   "Calls read-file-name, with the default being the file of the current
  807. tar-file descriptor."
  808.   (or prompt (setq prompt "Copy to: "))
  809.   (let* ((default-file (expand-file-name
  810.             (tar-header-name (tar-desc-tokens
  811.                       (tar-current-descriptor)))))
  812.      (target (expand-file-name
  813.           (read-file-name prompt
  814.                   (file-name-directory default-file)
  815.                   default-file nil))))
  816.     (if (or (string= "" (file-name-nondirectory target))
  817.         (file-directory-p target))
  818.     (setq target (concat (if (string-match "/$" target)
  819.                  (substring target 0 (1- (match-end 0)))
  820.                  target)
  821.                  "/"
  822.                  (file-name-nondirectory default-file))))
  823.     target))
  824.  
  825.  
  826. (defun tar-copy (&optional to-file)
  827.   "*In tar-mode, extract this entry of the tar file into a file on disk.
  828. If TO-FILE is not supplied, it is prompted for, defaulting to the name of
  829. the current tar-entry."
  830.   (interactive (list (tar-read-file-name)))
  831.   (let* ((descriptor (tar-current-descriptor))
  832.      (tokens (tar-desc-tokens descriptor))
  833.      (name (tar-header-name tokens))
  834.      (size (tar-header-size tokens))
  835.      (link-p (tar-header-link-type tokens))
  836.      (start (+ (tar-desc-data-start descriptor) tar-header-offset -1))
  837.      (end (+ start size)))
  838.     (if link-p (error "This is a link, not a real file."))
  839.     (if (zerop size) (error "This is a zero-length file."))
  840.     (let* ((tar-buffer (current-buffer))
  841.        buffer)
  842.       (unwind-protect
  843.       (progn
  844.         (setq buffer (generate-new-buffer "*tar-copy-tmp*"))
  845.         (widen)
  846.         (save-excursion
  847.           (set-buffer buffer)
  848.           (insert-buffer-substring tar-buffer start end)
  849.           (set-buffer-modified-p nil) ; in case we abort
  850.           (write-file to-file)
  851.           (message "Copied tar entry %s to %s" name to-file)
  852.           (set-buffer tar-buffer)))
  853.     (narrow-to-region 1 tar-header-offset)
  854.     (if buffer (kill-buffer buffer)))
  855.       )))
  856.  
  857.  
  858. (defun tar-flag-deleted (p &optional unflag)
  859.   "*In tar mode, mark this sub-file to be deleted from the tar file.
  860. With a prefix argument, mark that many files."
  861.   (interactive "p")
  862.   (beginning-of-line)
  863.   (tar-dotimes (i (if (< p 0) (- p) p))
  864.     (if (tar-current-descriptor unflag) ; barf if we're not on an entry-line.
  865.     (progn
  866.       (delete-char 1)
  867.       (insert (if unflag " " "D"))))
  868.     (forward-line (if (< p 0) -1 1)))
  869.   (if (eobp) nil (forward-char 36)))
  870.  
  871. (defun tar-unflag (p)
  872.   "*In tar mode, un-mark this sub-file if it is marked to be deleted.
  873. With a prefix argument, un-mark that many files forward."
  874.   (interactive "p")
  875.   (tar-flag-deleted p t))
  876.  
  877. (defun tar-unflag-backwards (p)
  878.   "*In tar mode, un-mark this sub-file if it is marked to be deleted.
  879. With a prefix argument, un-mark that many files backward."
  880.   (interactive "p")
  881.   (tar-flag-deleted (- p) t))
  882.  
  883.  
  884. (defun tar-expunge-internal ()
  885.   "Expunge the tar-entry specified by the current line."
  886.   (let* ((descriptor (tar-current-descriptor))
  887.      (tokens (tar-desc-tokens descriptor))
  888.      (line (tar-desc-data-start descriptor))
  889.      (name (tar-header-name tokens))
  890.      (size (tar-header-size tokens))
  891.      (link-p (tar-header-link-type tokens))
  892.      (start (tar-desc-data-start descriptor))
  893.      (following-descs (cdr (memq descriptor tar-parse-info))))
  894.     (if link-p (setq size 0)) ; size lies for hard-links.
  895.     ;;
  896.     ;; delete the current line...
  897.     (beginning-of-line)
  898.     (let ((line-start (point)))
  899.       (end-of-line) (forward-char)
  900.       (let ((line-len (- (point) line-start)))
  901.     (delete-region line-start (point))
  902.     ;;
  903.     ;; decrement the header-pointer to be in synch...
  904.     (setq tar-header-offset (- tar-header-offset line-len))))
  905.     ;;
  906.     ;; delete the data pointer...
  907.     (setq tar-parse-info (delq descriptor tar-parse-info))
  908.     ;;
  909.     ;; delete the data from inside the file...
  910.     (widen)
  911.     (let* ((data-start (+ start tar-header-offset -513))
  912.        (data-end (+ data-start 512 (ash (ash (+ size 511) -9) 9))))
  913.       (delete-region data-start data-end)
  914.       ;;
  915.       ;; and finally, decrement the start-pointers of all following
  916.       ;; entries in the archive.  This is a pig when deleting a bunch
  917.       ;; of files at once - we could optimize this to only do the
  918.       ;; iteration over the files that remain, or only iterate up to
  919.       ;; the next file to be deleted.
  920.       (let ((data-length (- data-end data-start)))
  921.     (tar-dolist (desc following-descs)
  922.       (tar-setf (tar-desc-data-start desc)
  923.             (- (tar-desc-data-start desc) data-length))))
  924.       ))
  925.   (narrow-to-region 1 tar-header-offset))
  926.  
  927.  
  928. (defun tar-expunge (&optional noconfirm)
  929.   "*In tar-mode, delete all the archived files flagged for deletion.
  930. This does not modify the disk image; you must save the tar file itself
  931. for this to be permanent."
  932.   (interactive)
  933.   (if (or noconfirm
  934.       (y-or-n-p "expunge files marked for deletion? "))
  935.       (let ((n 0))
  936.     (save-excursion
  937.       (goto-char 0)
  938.       (while (not (eobp))
  939.         (if (looking-at "D")
  940.         (progn (tar-expunge-internal)
  941.                (setq n (1+ n)))
  942.         (forward-line 1)))
  943.       ;; after doing the deletions, add any padding that may be necessary.
  944.       (tar-pad-to-blocksize)
  945.       (narrow-to-region 1 tar-header-offset)
  946.       )
  947.     (if (zerop n)
  948.         (message "nothing to expunge.")
  949.         (message "%s expunged.  Be sure to save this buffer." n)))))
  950.  
  951.  
  952. (defun tar-clear-modification-flags ()
  953.   "remove the stars at the beginning of each line."
  954.   (save-excursion
  955.     (goto-char 0)
  956.     (while (< (point) tar-header-offset)
  957.       (if (looking-at "*")
  958.       (progn (delete-char 1) (insert " ")))
  959.       (forward-line 1))))
  960.  
  961.  
  962. (defun tar-chown-entry (new-uid)
  963.   "*Change the user-id associated with this entry in the tar file.
  964. If this tar file was written by GNU tar, then you will be able to edit
  965. the user id as a string; otherwise, you must edit it as a number.
  966. You can force editing as a number by calling this with a prefix arg.
  967. This does not modify the disk image; you must save the tar file itself
  968. for this to be permanent."
  969.   (interactive (list
  970.          (let ((tokens (tar-desc-tokens (tar-current-descriptor))))
  971.            (if (or current-prefix-arg
  972.                (not (tar-header-magic tokens)))
  973.                (let (n)
  974.              (while (not (numberp (setq n (read-minibuffer
  975.                             "New UID number: "
  976.                             (format "%s" (tar-header-uid tokens)))))))
  977.              n)
  978.                (read-string "New UID string: " (tar-header-uname tokens))))))
  979.   (cond ((stringp new-uid)
  980.      (tar-setf (tar-header-uname (tar-desc-tokens (tar-current-descriptor)))
  981.            new-uid)
  982.      (tar-alter-one-field tar-uname-offset (concat new-uid "\000")))
  983.     (t
  984.      (tar-setf (tar-header-uid (tar-desc-tokens (tar-current-descriptor)))
  985.            new-uid)
  986.      (tar-alter-one-field tar-uid-offset
  987.        (concat (substring (format "%6o" new-uid) 0 6) "\000 ")))))
  988.  
  989.  
  990. (defun tar-chgrp-entry (new-gid)
  991.   "*Change the group-id associated with this entry in the tar file.
  992. If this tar file was written by GNU tar, then you will be able to edit
  993. the group id as a string; otherwise, you must edit it as a number.
  994. You can force editing as a number by calling this with a prefix arg.
  995. This does not modify the disk image; you must save the tar file itself
  996. for this to be permanent."
  997.   (interactive (list
  998.          (let ((tokens (tar-desc-tokens (tar-current-descriptor))))
  999.            (if (or current-prefix-arg
  1000.                (not (tar-header-magic tokens)))
  1001.                (let (n)
  1002.              (while (not (numberp (setq n (read-minibuffer
  1003.                             "New GID number: "
  1004.                             (format "%s" (tar-header-gid tokens)))))))
  1005.              n)
  1006.                (read-string "New GID string: " (tar-header-gname tokens))))))
  1007.   (cond ((stringp new-gid)
  1008.      (tar-setf (tar-header-gname (tar-desc-tokens (tar-current-descriptor)))
  1009.            new-gid)
  1010.      (tar-alter-one-field tar-gname-offset
  1011.        (concat new-gid "\000")))
  1012.     (t
  1013.      (tar-setf (tar-header-gid (tar-desc-tokens (tar-current-descriptor)))
  1014.            new-gid)
  1015.      (tar-alter-one-field tar-gid-offset
  1016.        (concat (substring (format "%6o" new-gid) 0 6) "\000 ")))))
  1017.  
  1018. (defun tar-rename-entry (new-name)
  1019.   "*Change the name associated with this entry in the tar file.
  1020. This does not modify the disk image; you must save the tar file itself
  1021. for this to be permanent."
  1022.   (interactive
  1023.     (list (read-string "New name: "
  1024.         (tar-header-name (tar-desc-tokens (tar-current-descriptor))))))
  1025.   (if (string= "" new-name) (error "zero length name."))
  1026.   (if (> (length new-name) 98) (error "name too long."))
  1027.   (tar-setf (tar-header-name (tar-desc-tokens (tar-current-descriptor)))
  1028.         new-name)
  1029.   (tar-alter-one-field 0
  1030.     (substring (concat new-name (make-string 99 0)) 0 99)))
  1031.  
  1032.  
  1033. (defun tar-chmod-entry (new-mode)
  1034.   "*Change the protection bits associated with this entry in the tar file.
  1035. This does not modify the disk image; you must save the tar file itself
  1036. for this to be permanent."
  1037.   (interactive (list (tar-parse-octal-integer-safe
  1038.                (read-string "New protection (octal): "))))
  1039.   (tar-setf (tar-header-mode (tar-desc-tokens (tar-current-descriptor)))
  1040.         new-mode)
  1041.   (tar-alter-one-field tar-mode-offset
  1042.     (concat (substring (format "%6o" new-mode) 0 6) "\000 ")))
  1043.  
  1044.  
  1045. (defun tar-alter-one-field (data-position new-data-string)
  1046.   (let* ((descriptor (tar-current-descriptor))
  1047.      (tokens (tar-desc-tokens descriptor)))
  1048.     (unwind-protect
  1049.     (save-excursion
  1050.       ;;
  1051.       ;; update the header-line.
  1052.       (beginning-of-line)
  1053.       (let ((p (point)))
  1054.         (forward-line 1)
  1055.         (delete-region p (point))
  1056.         (insert (summarize-tar-header-block tokens) "\n")
  1057.         (setq tar-header-offset (point-max)))
  1058.       
  1059.       (widen)
  1060.       (let* ((start (+ (tar-desc-data-start descriptor) tar-header-offset -513)))
  1061.         ;;
  1062.         ;; delete the old field and insert a new one.
  1063.         (goto-char (+ start data-position))
  1064.         (delete-region (point) (+ (point) (length new-data-string))) ; <--
  1065.         (insert new-data-string) ; <--
  1066.         ;;
  1067.         ;; compute a new checksum and insert it.
  1068.         (let ((chk (checksum-tar-header-block
  1069.             (buffer-substring start (+ start 512)))))
  1070.           (goto-char (+ start tar-chk-offset))
  1071.           (delete-region (point) (+ (point) 8))
  1072.           (insert (format "%6o" chk))
  1073.           (insert 0)
  1074.           (insert ? )
  1075.           (tar-setf (tar-header-checksum tokens) chk)
  1076.           ;;
  1077.           ;; ok, make sure we didn't botch it.
  1078.           (check-tar-header-block-checksum
  1079.             (buffer-substring start (+ start 512))
  1080.             chk (tar-header-name tokens))
  1081.           )))
  1082.       (narrow-to-region 1 tar-header-offset))))
  1083.  
  1084.  
  1085. (defun tar-subfile-save-buffer ()
  1086.   "In tar subfile mode, write this buffer back into its parent tar-file buffer.
  1087. This doesn't write anything to disk - you must save the parent tar-file buffer
  1088. to make your changes permanent."
  1089.   (interactive)
  1090.   (cond (buffer-file-name
  1091.      ;; tar-subfile buffers should have nil as buffer-file-name.  If they
  1092.      ;; ever gain a buffer-file-name, that means they have been written to
  1093.      ;; a real disk file, as with ^X^W.  If this happens, behave just like
  1094.      ;; `save-buffer.'
  1095.      (call-interactively 'save-buffer))
  1096.     (t
  1097.      (tar-subfile-save-buffer-internal))))
  1098.  
  1099. (defun tar-subfile-save-buffer-internal ()
  1100.   (if (not (and (boundp 'superior-tar-buffer) superior-tar-buffer))
  1101.       (error "this buffer has no superior tar file buffer."))
  1102.   (or (buffer-name superior-tar-buffer)
  1103.       (error "the superior tar file's buffer has been killed."))
  1104.   (if (not (and (boundp 'superior-tar-descriptor) superior-tar-descriptor))
  1105.       (error "this buffer doesn't have an index into its superior tar file!"))
  1106.  
  1107.   ;; Notice when crypt.el has uncompressed while reading the subfile, and
  1108.   ;; signal an error if the user tries to save back into the parent file
  1109.   ;; (because it won't work - the .Z subfile it writes won't really be
  1110.   ;; compressed.)
  1111.   ;;
  1112. ;  ;; These are for the old crypt.el
  1113. ;  (if (and (boundp 'buffer-save-encrypted) buffer-save-encrypted)
  1114. ;      (error "Don't know how to encrypt back into a tar file."))
  1115. ;  (if (and (boundp 'buffer-save-compacted) buffer-save-compacted)
  1116. ;      (error "Don't know how to compact back into a tar file."))
  1117. ;  (if (and (boundp 'buffer-save-compressed) buffer-save-compressed)
  1118. ;      (error "Don't know how to compress back into a tar file."))
  1119. ;  (if (and (boundp 'buffer-save-gzipped) buffer-save-gzipped)
  1120. ;      (error "Don't know how to gzip back into a tar file."))
  1121.  
  1122.   ;; These are for the new crypt++.el
  1123.   (if (and (boundp 'crypt-buffer-save-encrypted) crypt-buffer-save-encrypted)
  1124.       (error "Don't know how to encrypt back into a tar file."))
  1125.   (if (and (boundp 'crypt-buffer-save-compact) crypt-buffer-save-compact)
  1126.       (error "Don't know how to compact back into a tar file."))
  1127.   (if (and (boundp 'crypt-buffer-save-compress) crypt-buffer-save-compress)
  1128.       (error "Don't know how to compress back into a tar file."))
  1129.   (if (and (boundp 'crypt-buffer-save-gzip) crypt-buffer-save-gzip)
  1130.       (error "Don't know how to gzip back into a tar file."))
  1131.   (if (and (boundp 'crypt-buffer-save-freeze) crypt-buffer-save-freeze)
  1132.       (error "Don't know how to freeze back into a tar file."))
  1133.  
  1134.   (save-excursion
  1135.   (let ((subfile (current-buffer))
  1136.     (subfile-size (buffer-size))
  1137.     (descriptor superior-tar-descriptor))
  1138.     (set-buffer superior-tar-buffer)
  1139.     (let* ((tokens (tar-desc-tokens descriptor))
  1140.        (start (tar-desc-data-start descriptor))
  1141.        (name (tar-header-name tokens))
  1142.        (size (tar-header-size tokens))
  1143.        (size-pad (ash (ash (+ size 511) -9) 9))
  1144.        (head (memq descriptor tar-parse-info))
  1145.        (following-descs (cdr head)))
  1146.       (if (not head)
  1147.     (error "Can't find this tar file entry in its parent tar file!"))
  1148.       (unwind-protect
  1149.        (save-excursion
  1150.     (widen)
  1151.     ;; delete the old data...
  1152.     (let* ((data-start (+ start tar-header-offset -1))
  1153.            (data-end (+ data-start (ash (ash (+ size 511) -9) 9))))
  1154.       (delete-region data-start data-end)
  1155.       ;; insert the new data...
  1156.       (goto-char data-start)
  1157.       (insert-buffer subfile)
  1158.       ;;
  1159.       ;; pad the new data out to a multiple of 512...
  1160.       (let ((subfile-size-pad (ash (ash (+ subfile-size 511) -9) 9)))
  1161.         (goto-char (+ data-start subfile-size))
  1162.         (insert (make-string (- subfile-size-pad subfile-size) 0))
  1163.         ;;
  1164.         ;; update the data pointer of this and all following files...
  1165.         (tar-setf (tar-header-size tokens) subfile-size)
  1166.         (let ((difference (- subfile-size-pad size-pad)))
  1167.           (tar-dolist (desc following-descs)
  1168.         (tar-setf (tar-desc-data-start desc)
  1169.               (+ (tar-desc-data-start desc) difference))))
  1170.         ;;
  1171.         ;; Update the size field in the header block.
  1172.         (let ((header-start (- data-start 512)))
  1173.           (goto-char (+ header-start tar-size-offset))
  1174.           (delete-region (point) (+ (point) 12))
  1175.           (insert (format "%11o" subfile-size))
  1176.           (insert ? )
  1177.           ;;
  1178.           ;; Maybe update the datestamp.
  1179.           (if (not tar-update-datestamp)
  1180.           nil
  1181.         (goto-char (+ header-start tar-time-offset))
  1182.         (delete-region (point) (+ (point) 12))
  1183.         (let (now top bot)
  1184.           (cond ((fboundp 'current-time)
  1185.              (setq now (current-time))
  1186.              (setcdr now (car (cdr now))))
  1187. ;            ((fboundp 'current-time-seconds)
  1188. ;             (setq now (current-time-seconds)))
  1189.             )
  1190.           (setq top (car now)
  1191.             bot (cdr now))
  1192.           (cond
  1193.            (now
  1194.             (tar-setf (tar-header-date tokens) now)
  1195.             ;; hair to print two 16-bit numbers as one octal number.
  1196.             (setq bot (logior (ash (logand top 3) 16) bot))
  1197.             (setq top (ash top -2))
  1198.             (insert (format "%5o" top))
  1199.             (insert (format "%06o " bot)))
  1200.            (t
  1201.             ;; otherwise, set it to the epoch.
  1202.             (insert (format "%11o " 0))
  1203.             (tar-setf (tar-header-date tokens) (cons 0 0))
  1204.             ))))
  1205.           ;;
  1206.           ;; compute a new checksum and insert it.
  1207.           (let ((chk (checksum-tar-header-block
  1208.               (buffer-substring header-start data-start))))
  1209.         (goto-char (+ header-start tar-chk-offset))
  1210.         (delete-region (point) (+ (point) 8))
  1211.         (insert (format "%6o" chk))
  1212.         (insert 0)
  1213.         (insert ? )
  1214.         (tar-setf (tar-header-checksum tokens) chk)))
  1215.         ;;
  1216.         ;; alter the descriptor-line...
  1217.         ;;
  1218.         (let ((position (- (length tar-parse-info) (length head))))
  1219.           (goto-char 1)
  1220.           (next-line position)
  1221.           (beginning-of-line)
  1222.           (let ((p (point))
  1223.             (m (set-marker (make-marker) tar-header-offset)))
  1224.         (forward-line 1)
  1225.         (delete-region p (point))
  1226.         (insert-before-markers (summarize-tar-header-block tokens t) "\n")
  1227.         (setq tar-header-offset (marker-position m)))
  1228.           )))
  1229.     ;; after doing the insertion, add any final padding that may be necessary.
  1230.     (tar-pad-to-blocksize))
  1231.        (narrow-to-region 1 tar-header-offset)))
  1232.     (set-buffer-modified-p t)   ; mark the tar file as modified
  1233.     (set-buffer subfile)
  1234.     (set-buffer-modified-p nil) ; mark the tar subfile as unmodified
  1235.     (message "saved into tar-buffer \"%s\" - remember to save that buffer!"
  1236.          (buffer-name superior-tar-buffer))
  1237.     )))
  1238.  
  1239.  
  1240. (defun tar-pad-to-blocksize ()
  1241.   "If we are being anal about tar file blocksizes, fix up the current buffer.
  1242. Leaves the region wide."
  1243.   (if (null tar-anal-blocksize)
  1244.       nil
  1245.     (widen)
  1246.     (let* ((last-desc (nth (1- (length tar-parse-info)) tar-parse-info))
  1247.        (start (tar-desc-data-start last-desc))
  1248.        (tokens (tar-desc-tokens last-desc))
  1249.        (link-p (tar-header-link-type tokens))
  1250.        (size (if link-p 0 (tar-header-size tokens)))
  1251.        (data-end (+ start size))
  1252.        (bbytes (ash tar-anal-blocksize 9))
  1253.        (pad-to (+ bbytes (* bbytes (/ (1- data-end) bbytes))))
  1254.        (buffer-read-only nil) ; ##
  1255.        )
  1256.       ;; If the padding after the last data is too long, delete some;
  1257.       ;; else insert some until we are padded out to the right number of blocks.
  1258.       ;;
  1259.       (goto-char (+ (or tar-header-offset 0) data-end))
  1260.       (if (> (1+ (buffer-size)) (+ (or tar-header-offset 0) pad-to))
  1261.       (delete-region (+ (or tar-header-offset 0) pad-to) (1+ (buffer-size)))
  1262.       (insert (make-string (- (+ (or tar-header-offset 0) pad-to)
  1263.                   (1+ (buffer-size)))
  1264.                    0)))
  1265.       )))
  1266.  
  1267.  
  1268. (defun maybe-write-tar-file ()
  1269.   "Used as a write-file-hook to write tar-files out correctly."
  1270.   ;;
  1271.   ;; If the current buffer is in tar-mode and has its header-offset set,
  1272.   ;; remove the header from the file, call the remaining write-file hooks,
  1273.   ;; and then write out the buffer (if and only if one of the write-file
  1274.   ;; hooks didn't write it already).  Then put the header back on the
  1275.   ;; buffer.  Many thanks to Piet van Oostrum for this code, which causes
  1276.   ;; correct interaction with crypt.el (and probably anything like it.)
  1277.   ;;
  1278.   ;; Kludge: in XEmacs Emacs, write-file-hooks is bound to nil before the
  1279.   ;; write-file-hooks are run, to prevent them from being run recursively
  1280.   ;; (this is more of a danger in v19-vintage emacses, which have both
  1281.   ;; write-file-hooks and write-contents-hooks.)  So, we need to reference
  1282.   ;; an internal variable of basic-save-buffer to get the list of hooks
  1283.   ;; remaining to be run.
  1284.   ;;
  1285.   (and (eq major-mode 'tar-mode)
  1286.        (and (boundp 'tar-header-offset) tar-header-offset)
  1287.        (let* ((hooks (cond ((string-match "XEmacs" emacs-version)
  1288.                 ;; Internal to basic-save-buffer in XEmacs.
  1289.                 (symbol-value 'hooks))
  1290.                ((string-lessp "19" emacs-version)
  1291.                 ;; I think this is what we need to do in fsfmacs.
  1292.                 (append write-contents-hooks write-file-hooks))
  1293.                (t
  1294.                 write-file-hooks)))
  1295.           (remaining-hooks (cdr (memq 'maybe-write-tar-file hooks)))
  1296.           header-string
  1297.           done)
  1298.      (save-excursion
  1299.       (save-restriction
  1300.        (widen)
  1301.        (tar-clear-modification-flags)
  1302.        (setq header-string (buffer-substring 1 tar-header-offset))
  1303.        (delete-region 1 tar-header-offset)
  1304.        (unwind-protect
  1305.            (progn
  1306.          (while (and remaining-hooks
  1307.                  (not (setq done (funcall (car remaining-hooks)))))
  1308.            (setq remaining-hooks (cdr remaining-hooks)))
  1309.          (cond ((not done)
  1310.             (write-region 1 (1+ (buffer-size))
  1311.                      buffer-file-name nil t)
  1312.             (setq done t))))
  1313.          (goto-char 1)
  1314.          (insert header-string)
  1315.          (set-buffer-modified-p nil))))
  1316.      done)))
  1317.  
  1318.  
  1319. ;;; Patch it in.
  1320.  
  1321. (defvar tar-regexp "\\.\\(tar\\|tgz\\)$"
  1322.   "The regular expression used to identify tar file names.")
  1323.  
  1324. (setq auto-mode-alist
  1325.       (cons (cons tar-regexp 'tar-mode) auto-mode-alist))
  1326.  
  1327. ;; Note: the tar write-file-hook should go on the list *before* any other
  1328. ;; hooks which might write the file.  Since things like crypt-mode add things
  1329. ;; to the end of the write-file-hooks, this will normally be the case.
  1330.  
  1331. ;(or (boundp 'write-file-hooks) (setq write-file-hooks nil))
  1332. ;(or (listp write-file-hooks)
  1333. ;    (setq write-file-hooks (list write-file-hooks)))
  1334. ;(or (memq 'maybe-write-tar-file write-file-hooks)
  1335. ;    (setq write-file-hooks
  1336. ;      (cons 'maybe-write-tar-file write-file-hooks)))
  1337.  
  1338. (add-hook 'write-file-hooks 'maybe-write-tar-file); ####write-contents-hooks??
  1339. (cond ((boundp 'after-save-hook)
  1340.        (add-hook 'after-save-hook 'tar-subfile-after-write-file-hook))
  1341.       ((boundp 'after-write-file-hooks)
  1342.        (add-hook 'after-write-file-hooks 'tar-subfile-after-write-file-hook))
  1343.       (t (error "neither after-save-hook nor after-write-file-hooks?")))
  1344.  
  1345.  
  1346. ;;; This is a hack.  For files ending in .tar, we want -*- lines to be
  1347. ;;; completely ignored - if there is one, it applies to the first file
  1348. ;;; in the archive, and not the archive itself!  Similarly for local
  1349. ;;; variables specifications in the last file of the archive.
  1350.  
  1351. (defun tar-normal-mode (&optional find-file)
  1352.   "Choose the major mode for this buffer automatically.
  1353. Also sets up any specified local variables of the file.
  1354. Uses the visited file name, the -*- line, and the local variables spec.
  1355.  
  1356. This function is called automatically from `find-file'.  In that case,
  1357. if `inhibit-local-variables' is non-`nil' we require confirmation before
  1358. processing a local variables spec.  If you run `normal-mode' explicitly,
  1359. confirmation is never required.
  1360.  
  1361. Note that this version of this function has been hacked to interact
  1362. correctly with tar files - when visiting a file which matches
  1363. 'tar-regexp', the -*- line and local-variables are not examined,
  1364. as they would apply to a file within the archive rather than the archive
  1365. itself."
  1366.   (interactive)
  1367.   (if (and buffer-file-name
  1368.        (string-match tar-regexp buffer-file-name))
  1369.       (tar-mode)
  1370.       (tar-real-normal-mode find-file)))
  1371.  
  1372. ;; We have to shadow this as well to get along with crypt.el.
  1373. ;; Shadowing this alone isn't enough, though; we need to shadow 
  1374. ;; tar-normal-mode in order to inhibit the local variables of the
  1375. ;; last file in the tar archive.
  1376. ;;
  1377. (defun tar-set-auto-mode ()
  1378.   "Select major mode appropriate for current buffer.
  1379. May base decision on visited file name (See variable  auto-mode-list)
  1380. or on buffer contents (-*- line or local variables spec), but does not look
  1381. for the \"mode:\" local variable.  For that, use  hack-local-variables.
  1382.  
  1383. Note that this version of this function has been hacked to interact
  1384. correctly with tar files - when visiting a file which matches
  1385. 'tar-regexp', the -*- line and local-variables are not examined,
  1386. as they would apply to a file within the archive rather than the archive
  1387. itself."
  1388.   (interactive)
  1389.   (if (and buffer-file-name
  1390.        (string-match tar-regexp buffer-file-name))
  1391.       (tar-mode)
  1392.       (tar-real-set-auto-mode)))
  1393.  
  1394. (if (not (fboundp 'tar-real-normal-mode))
  1395.     (fset 'tar-real-normal-mode (symbol-function 'normal-mode)))
  1396. (fset 'normal-mode 'tar-normal-mode)
  1397.  
  1398. (if (not (fboundp 'tar-real-set-auto-mode))
  1399.     (fset 'tar-real-set-auto-mode (symbol-function 'set-auto-mode)))
  1400. (fset 'set-auto-mode 'tar-set-auto-mode)
  1401.  
  1402. (provide 'tar-mode)
  1403.